import { create, update, remove, query,createRaw } from '../../../../../framework/service/index';
import { Toast } from 'antd-mobile';
import { routerRedux } from 'dva/router';

const delay = timeout => {
  return new Promise(resolve => {
    setTimeout(resolve, timeout);
  });
};

export default {

  namespace: 'checkRealName',

  state: {
    date: [],
    profile: {},
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if(location.pathname === '/check-real-name') {
          dispatch({
            type: 'query',
          })
        }
      })
    },
  },



  effects: {
    *query({ payload }, { call, put }) {
      const { code, data, message } = yield call(query, '/api/applicants/details', {});
      // console.log('message', JSON.stringify(message));

      if ( code == 200 && data ) {
        yield put({
          type: 'querySuccess',
          payload: {
            profile: data.profile,
            resume: data.resume,
          }
        })
      }
    },
    *onUploadImg({ payload }, { call, put,select }) {

      const profile = yield select (({checkRealName}) => checkRealName.profile);

      const { code, data, message } = yield call(createRaw, '/api/upload64?blur=false', payload.data.base64Url);
      let imgData = {}
      if ( code == 200 && data ) {
        if (payload.data.key=="idcardFront") {
          imgData = {idcardFront: data.url}
        } else if (payload.data.key=="idcardBack") {
          imgData = {idcardBack: data.url}
        } else if (payload.data.key=="bankcardSnapshot") {
          imgData = {bankcardSnapshot: data.url}
        }

        const result =  Object.assign(profile, imgData);
        console.log('修改图片', result);
        yield put({ type: 'querySuccess',payload:{profile:result}});

      }

      // yield put({ type: 'querySuccess',payload:{imgURL:payload.imgURL}});
    },

    *onSubmit({ payload }, { call, put,select }) {

      const profile = yield select (({checkRealName}) => checkRealName.profile);
      // console.log('profileprofile',profile);
      // console.log('提交', payload);
      const result =  Object.assign(profile, payload.data);
      // console.log('数据',result);
      const { code, data, message } = yield call(update, '/api/applicant/profile', result);
      if ( code == 200 ) {
        Toast.info('已提交认证', 1.5)
        yield put(routerRedux.goBack())
      }
    },
  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
  }

};
